#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <map>
#include <list>
#include <string>


#pragma pack(1)
class PWR_ITEM{
public:
	unsigned int date;
	std::string info;
};

typedef std::list<PWR_ITEM * > PWR_LIST;
typedef std::map<std::string, PWR_LIST * > PWR_MAP;
PWR_MAP pwr_map;


typedef struct
{
	int date;
	int open;
	int high;
	int low;
	int close;
	int amount;
	int vol;
	int reservation;
} StockData; 

typedef struct
{
	unsigned int date;
	float open;
	float high;
	float low;
	float close;
	float amount;
	float vol;
} StockDayItem; 

typedef struct{
	char ip[42];
	unsigned int date;
	unsigned int time;
}dat_head;

typedef struct{
	char code[24];
	char name[24];
	char dumy[195];
	char sc[9];
}dat_item;

typedef struct{
	char code[23];
	char name[23];
	char dumy[239];
	char sc[29];
}dat_item2;
char end = 0;

StockData stockData;
int read_data(FILE * fp, StockDayItem * &pdata, int & ndata);
int read_tnf(char * file_name);
int read_tnf2(char * file_name);

void free_pwr_map()
{
   PWR_MAP::iterator it = pwr_map.begin();
   for(;it != pwr_map.end(); it++) {
	   PWR_LIST * pwr_list = it->second;
	   PWR_LIST::iterator it_list = pwr_list->begin();
	   for( ;it_list != pwr_list->end(); it_list ++) {
		   delete *it_list;
	   }
	   delete pwr_list;
   }
}



PWR_LIST * get_pwr_list(char * code)
{
	return pwr_map[code];
}

char *  prefix_pwr = "D:\\tools\\dzh365(64)\\Download\\PWR\\";
char *  tc_file[]={"full_sh.PWR","full_sz.PWR"};

int load_pwr_map()
{
	char name[1024];

	for( int i=0; i< 2; i++ ) {
		strcpy(name, prefix_pwr);
		strcat(name, tc_file[i]);
		read_tnf(name);
	}

	return 0;
}

int main(int argc,char *argv[])
{
   load_pwr_map();

   for(;;) {
	char code[1024];
	code[0]= 0;
    printf("\n please input stock code, q to quit:\n");
	scanf("%s",code);
	if(code[0] == 'q')
		break;
	PWR_LIST * pwr_list = get_pwr_list(code);
	if(pwr_list) {
		PWR_LIST::iterator it_list = pwr_list->begin();
		for( ;it_list != pwr_list->end(); it_list ++) {
			printf("\n %d %s",(*it_list)->date, (*it_list)->info.c_str());
		}
	}
	else {
		printf("\n%s not found!", code);
	}
   }

   free_pwr_map();
}
int read_tnf(char * file_name)
{
	FILE *fp;
	if((fp = fopen(file_name,"rb")) == NULL)
	{
		printf("Error: Can't open :%s! ",file_name);
		return -1;
	}
	int ndata = 0;

	char head[8];
	fread(&head, sizeof(char)* 8,1, fp);
	PWR_LIST * cur_pwr_list = NULL;
	char code[116];

	for(;;) {
		unsigned int date;
		if(fread(&date, sizeof(int), 1, fp ) != 1)
			break;
		if(date == 0xffffffff) {
			if(cur_pwr_list) {
				pwr_map[code+2]= cur_pwr_list;
			}
			cur_pwr_list = new  PWR_LIST;

			if(fread(code, 116, 1, fp) != 1)
				break;
#ifdef P_DEBUG
			printf("\ncode: %s",code);
#endif
		}
		else {
			time_t tt = date;
			struct tm * t = localtime((const time_t *)&tt);
			char info[116];
			if(fread(info, 116, 1, fp) != 1)
				break;

			PWR_ITEM * pwr_item = new PWR_ITEM;
			pwr_item->info = info + 16;
			pwr_item->date = (t->tm_year + 1900) * 10000 + (t->tm_mon + 1) * 100 + t-> tm_mday;
#ifdef P_DEBUG
			printf("\n%d %d %d %s ",t->tm_year + 1900 ,t->tm_mon + 1, t-> tm_mday, info + 16);
			printf("\n%d %s", pwr_item->date, pwr_item->info.c_str());
#endif
			cur_pwr_list->push_back(pwr_item);
		}
	}

	fclose(fp);

#ifdef P_DEBUG
	if(getch()==0) getch();
#endif

	return 0;
}


